//
//  HotSellCollectionViewCell.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/29.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class HotSellCollectionViewCell: UICollectionViewCell {
    
    var model:ItemModel?
    {
        didSet
        {
            guard let item = model else {
                
                return
            }
            
            if let tags = item.tags {
                
                if tags.count == 0 {
                    
                    self.attLabel.isHidden = true
                }
                else
                {
                    self.attLabel.isHidden = false
                    let tmodel:ItemAttrExtModel = tags.first!
                    self.attLabel.text = tmodel.name
                    self.attLabel.textColor = UIColor.white
                    self.attLabel.backgroundColor = UIColor.hexColor(hexadecimal: tmodel.color!)
                }
            }
            
            self.imageView.kf.setImage(with: URL(string:item.pic_url ?? ""))
            self.subTitleLabel.text = item.summary ?? ""
            self.priceLabel.text = "¥\(item.price_min/100)"
            self.titleLabel.text = item.name ?? ""
        }
    }
    
    lazy var imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.hexColor(hexadecimal: "#fafafa")
        self.addSubview(imageView)
        return imageView
    }()
    
    private lazy var titleLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.hexColor(hexadecimal: "#000000")
        label.textAlignment = NSTextAlignment.left
        self.addSubview(label)
        return label
    }()
    private lazy var subTitleLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.hexColor(hexadecimal: "#666666")
        label.textAlignment = NSTextAlignment.center
        self.addSubview(label)
        return label
    }()
    
    private lazy var attLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.hexColor(hexadecimal: "#666666")
        label.textAlignment = NSTextAlignment.center
        self.addSubview(label)
        return label
    }()
    
    private lazy var priceLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor =  UIColor.hexColor(hexadecimal: "#f62700")
        label.textAlignment = NSTextAlignment.center
        self.addSubview(label)
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.backgroundColor = UIColor.white
        self.contentView.layer.cornerRadius = 5;
        let imgWidth:CGFloat = 100
        
        self.imageView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: imgWidth, height: imgWidth))
        }
        
        self.titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(5)
            make.right.lessThanOrEqualTo(-5)
            make.top.equalTo(self.imageView.snp_bottom).offset(8)
        }
        
        self.priceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(5)
            make.top.equalTo(self.titleLabel.snp_bottom).offset(10)
        }

        self.attLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.height.equalTo(15)
            make.width.greaterThanOrEqualTo(30)
            make.bottom.equalTo(self.imageView.snp_bottom)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
